package com.yunhoo.base.bean;

import com.yunhoo.base.model.ClassCode;
import com.yunhoo.base.model.MsgModel;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.pay.util.ToolsMethed;
import com.yunhoo.sys.util.common.LoginParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.richfaces.component.UIDataTable;   

/**
 *
 * @author Administrator
 */
@ManagedBean(name ="class")
@SessionScoped
public class ClassCodeBean {

    private ClassCode classcode = new ClassCode();
    private List<ClassCode> classlist = new ArrayList();
    private DataModel classModel = new ListDataModel();
    private List<SelectItem> selectItemList;
    private UIDataTable data = null;
    private boolean showAble = true;
    private boolean addOrUpdate = false;
    private boolean editable = true;
    private static String nameSpace = "classCode.";

    public ClassCodeBean() {
        classcode = new ClassCode();
        selectItemList = new ArrayList();
        classlist = IbatisTemplate.queryForList(nameSpace + "queryClassCodeAll", classcode);
        classModel.setWrappedData(classlist);
        for (ClassCode c : classlist) {
            selectItemList.add(new SelectItem(c.getCode(), c.getName()));
        }
        initStatus();
    }

    public void initStatus() {
        showAble = true;
        addOrUpdate = false;
        editable = true;
        classcode = new ClassCode();
        System.out.println(classcode);
        classlist = IbatisTemplate.queryForList(nameSpace + "queryClassCodeAll", classcode);
        classModel.setWrappedData(classlist);
        if (classcode.getCode().trim().equals("")) {
            System.out.println("true");
        }
    }

    public void add() {
        classcode.setCode(ToolsMethed.getClassCodeMaxNumber());
        showAble = false;
        addOrUpdate = true;
        editable = false;
        classcode.setName(null);
        //this.setClasscode(new ClassCode());
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void update() {
        System.out.println("进入修改状态");
        showAble = false;
        addOrUpdate = false;
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void checkRpt() {
        if (!classcode.getCode().equals("")) {
            if (addOrUpdate) {
                List<ClassCode> repeat;
                Map map = new HashMap();
                map.put("name", classcode.getName().trim());
                map.put("orgCode",LoginParam.getOrg().getCode());
                map.put("iyear", LoginParam.getIyear());
                repeat = (List<ClassCode>) IbatisTemplate.queryForList(nameSpace + "queryClassCodeForRpt", map);
                System.out.println("repeat:" + repeat.size());
                System.out.println(repeat);
                if (repeat != null && repeat.size() > 0) {
                   ToolKit.msgs_err("标准代码或名称重复!");
                    System.out.println("标准代码或名称重复!");
                } else {
                    //save();
                    IbatisTemplate.insert(nameSpace + "insertClassCode", classcode);
                }
            }
            if (!addOrUpdate) {
               // save();
                 IbatisTemplate.update(nameSpace + "updateClassCode", classcode);
            }
            initStatus();
        } else {
            ToolKit.msgs_err("请输入代码或名称!");
            System.out.println("请输入代码或名称!");
        }

    }

    public void save() {
        if (addOrUpdate) { //新增保存
            IbatisTemplate.insert(nameSpace + "insertClassCode", classcode);
        }
        if (!addOrUpdate) {//修改保存
            System.out.print("准备修改的是:" + classcode.getCode());
            IbatisTemplate.update(nameSpace + "updateClassCode", classcode);
        }
        initStatus();
    }

    public void delete() {
        IbatisTemplate.delete(nameSpace + "deleteClassCode", classcode);
        initStatus();
    }
    public void checkDelete(){
        Map map = new HashMap();
        map.put("orgCode",LoginParam.getOrg().getCode());
        map.put("code",classcode.getCode().trim());
        map.put("iyear", LoginParam.getIyear());
        MsgModel msg = (MsgModel) IbatisTemplate.query(nameSpace + "selectClassMsg", map);
        System.out.println("msg:" + msg.getMsg());
        if (msg.getMsg().equals("")) {
            delete();
        } else {
           ToolKit.msgs_err("已在表" + msg.getMsg() + "中使用");
        }
    }

    public void show() {
        this.setClasscode((ClassCode) this.classModel.getRowData());
        FacesContext.getCurrentInstance().renderResponse();
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public ClassCode getClasscode() {
        return classcode;
    }

    public void setClasscode(ClassCode classcode) {
        this.classcode = classcode;
    }

    public List<ClassCode> getClasslist() {
        return classlist;
    }

    public void setClasslist(List<ClassCode> classlist) {
        this.classlist = classlist;
    }

    public UIDataTable getData() {
        return data;
    }

    public void setData(UIDataTable data) {
        this.data = data;
    }

   

    public boolean isShowAble() {
        return showAble;
    }

    public void setShowAble(boolean showAble) {
        this.showAble = showAble;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public List<SelectItem> getSelectItemList() {
        return selectItemList;
    }

    public void setSelectItemList(List<SelectItem> selectItemList) {
        this.selectItemList = selectItemList;
    }

    public DataModel getClassModel() {
        return classModel;
    }

    public void setClassModel(DataModel classModel) {
        this.classModel = classModel;
    }
}
